﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using School.Entities;
using School.Common;
using System.Data.Entity;
using School.Admission;

namespace School.Pages.Admission
{
    public partial class Transfer_Request : System.Web.UI.Page
    {
        //***************** This is function to handle page when page load ***************************
        //-**********************************************************************
        DataTable dt = null;
        int i = 1;

        Int64 mytrid;

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1022) == true)
                {
                    if (!(Page.IsPostBack))
                    {

                        Fillcrls();

                        InitializeControls();

                        GridDataBind();

                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        # region "Collect Data"

        //***************** This is function to bind data grid  ***************************
        //-**************************************************************************
        protected void GridDataBind()
        {
            Int32 mycount;
            Hashtable SearchTable = new Hashtable();

            if (cbStatus.Checked == true)
            {
                SearchTable.Add("Status", "2");

            }
            else
            {
                SearchTable.Add("Status", "1");
            }


            if (txtSearchFirstName.Text.Length > 0)
            {
                SearchTable.Add("FirstName", txtSearchFirstName.Text.ToString());
            }

            if (txtSearchSecondName.Text.Length > 0)
            {
                SearchTable.Add("SecondName", txtSearchSecondName.Text.ToString());
            }


            if (txtSearchThirdName.Text.Length > 0)
            {
                SearchTable.Add("ThirdName", txtSearchThirdName.Text.ToString());
            }



            if (txtSearchFourthName.Text.Length > 0)
            {
                SearchTable.Add("FourthName", txtSearchFourthName.Text.ToString());
            }

            if (txtSearchDate.Text.Length > 0)
            {
                SearchTable.Add("Date", txtSearchDate.Text.ToString());
            }


            if (ddlSearchcampus_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Campus_Id", ddlSearchcampus_Id.SelectedValue);


            }

            if (ddlSearchCertificate_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Certificate_Id", ddlSearchCertificate_Id.SelectedValue);


            }

            if (ddlSearchStage_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Stage_Id", ddlSearchStage_Id.SelectedValue);


            }

            if (ddlSearchGrade_ID.SelectedIndex > 0)
            {
                SearchTable.Add("Grade_Id", ddlSearchGrade_ID.SelectedValue);


            }


            griddata.DataSource = TransferRequest.All(SearchTable);

            griddata.DataBind();
            griddata.Visible = true;
            mycount = griddata.Rows.Count;
            lblRecordNo.Text = "عدد نتائج البحث = " + mycount.ToString();
            if (mycount == 0)
            {

                btnAccept.Visible = false;
                btnReject.Visible = false;
                lblRecordNo.Visible = false;
                lblMessage.Text = "No search result(s) found, please try again.";
                griddata.Visible = false;
            }
            else
            {
                lblMessage.Text = "";
                btnAccept.Visible = true;
                btnReject.Visible = true;
                griddata.Visible = true;

                lblRecordNo.Visible = true;
            }

        }

        # endregion


        # region "Fill all crls"

        //***************** This is function to fill crl  ***************************
        //-**************************************************************************

        protected void Fillcrls()
        {

            ddlcampus_Id.DataSource = Structure.GetCampuses();
            ddlcampus_Id.DataTextField = "Name";
            ddlcampus_Id.DataValueField = "Id";
            ddlcampus_Id.DataBind();
            ddlcampus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlSearchcampus_Id.DataSource = Structure.GetCampuses();
            ddlSearchcampus_Id.DataTextField = "Name";
            ddlSearchcampus_Id.DataValueField = "Id";
            ddlSearchcampus_Id.DataBind();
            ddlSearchcampus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));




            ddlFirstLanguage.DataSource = LookUps.Languages();
            ddlFirstLanguage.DataTextField = "Name";
            ddlFirstLanguage.DataValueField = "Id";
            ddlFirstLanguage.DataBind();


            ddlSecondLanguage.DataSource = LookUps.Languages();
            ddlSecondLanguage.DataTextField = "Name";
            ddlSecondLanguage.DataValueField = "Id";
            ddlSecondLanguage.DataBind();

            ddlCertification.DataSource = Structure.GetCertificates();
            ddlCertification.DataTextField = "Name";
            ddlCertification.DataValueField = "Id";
            ddlCertification.DataBind();

        }


        //***************** This is function to initlaize crl  ***************************
        //-**************************************************************************
        protected void InitializeControls()
        {

            ddlSearchcampus_Id.SelectedIndex = 0;

            ddlSearchCertificate_Id.Items.Clear();
            ddlSearchStage_Id.Items.Clear();
            ddlSearchGrade_ID.Items.Clear();
            ddlSearchCertificate_Id.Enabled = false;
            ddlSearchStage_Id.Enabled = false;
            ddlSearchGrade_ID.Enabled = false;
            txtFirstName.Text = "";
            txtSecondName.Text = "";
            txtThirdName.Text = "";
            txtFourthName.Text = "";
            txtDate.Text = "";




        }

        # endregion


        # region "Mode Handlers"

        //***************** This is function to handle page in add mode  ***************************
        //-**************************************************************************
        protected void PutFormInAddMode()
        {
            txtFirstName.Text = "";
            txtSecondName.Text = "";
            txtThirdName.Text = "";
            txtFourthName.Text = "";
        }


        //***************** This is function to handle page in brows mode  ***************************
        //-**************************************************************************

        protected void PutFormInBrwosMode()
        {
            pnlAdd.Visible = false;
            pnlbrows.Visible = false;

        }

        #endregion


        # region "Grid Events ***********"

        //***************** This is function to handle page when Page Index Changing   ***************************
        //-**************************************************************************

        protected void griddata_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            griddata.PageIndex = e.NewPageIndex;
            GridDataBind();
        }


        //***************** This is function to handle page when Row Deleting  ***************************
        //-**************************************************************************




        //***************** This is function to handle page when Row Data Bound  ***************************
        //-**************************************************************************
        protected void griddata_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (cbStatus.Checked == true)
            {
                if (griddata.Rows.Count > 0)
                {

                    griddata.Columns[4].Visible = true;
                    griddata.Columns[3].Visible = false;
                }
            }

            else
            {
                griddata.Columns[4].Visible = false;
                griddata.Columns[3].Visible = true;

            }
        }


        # endregion



        # region   "Execute commands sent to the form"



        //***************** This is function to handle page when Puser click in الغاء btn  ***************************
        //-**************************************************************************
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtFirstName.Text = "";
            txtSecondName.Text = "";
            txtThirdName.Text = "";
            txtFourthName.Text = "";
            txtDate.Text = "";


            ddlcampus_Id.SelectedIndex = 0;
            //ddlCertificate_Id.SelectedIndex = 0;
            //ddlGrade_Id.SelectedIndex = 0;
            //ddlStage_Id.SelectedIndex = 0;

        }


        //***************** This is function to handle page when Puser click in اضافة btn  ***************************
        //-**************************************************************************

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            pnlAdd.Visible = true;
            pnlbrows.Visible = false;

        }

        //***************** This is function to handle page when Puser click in انهاء btn  ***************************
        //-**************************************************************************


        protected void btnend_Click(object sender, EventArgs e)
        {
            btnAdd.Visible = true;
            pnlAdd.Visible = false;
            pnlbrows.Visible = true;
        }
        //***************** This is function to handle page when Puser click in حفظ btn  ***************************
        //-**************************************************************************

        protected void btnSave_Click(object sender, EventArgs e)
        {

            pnlAddNewXSchools.Visible = false;
            Int64 campus = Convert.ToInt64(ddlcampus_Id.SelectedValue);
            Int64 certifcate = Convert.ToInt64(ddlCertificate_Id.SelectedValue);
            Int64 Stage = Convert.ToInt64(ddlStage_Id.SelectedValue);
            Int64 grade = Convert.ToInt64(ddlGrade_Id.SelectedValue);

            DateTime dt = Convert.ToDateTime(txtDate.Text);

            mytrid = TransferRequest.Add(new ADM_Transfer_Request()
            {
                FirstName = txtFirstName.Text,
                SecondName = txtSecondName.Text,
                ThirdName = txtThirdName.Text,
                FourthName = txtFourthName.Text,
                Date = dt,
                Campus_Id = campus,

                Certificate_Id = certifcate,
                Stage_Id = Stage,
                Grade_Id = grade,
                Status = 1



            }
           );

            AddXSchoolToStudent();

            lblMessage.Text = "Record(s) was added successfully";
            pnlAdd.Visible = false;
            pnlbrows.Visible = true;

            GridDataBind();
        }



        protected void AddXSchoolToStudent()
        {
            int j;
            for (j = 0; j <= griddataxschools.Rows.Count - 1; j++)
            {


                String mySchoolName = griddataxschools.Rows[j].Cells[3].Text;
                DateTime mySatrtDate = Convert.ToDateTime(griddataxschools.Rows[j].Cells[4].Text);
                DateTime myEndDate = Convert.ToDateTime(griddataxschools.Rows[j].Cells[5].Text);
                String myAddress = griddataxschools.Rows[j].Cells[6].Text;
                String myCertification = griddataxschools.Rows[j].Cells[7].Text;
                String myGrade = griddataxschools.Rows[j].Cells[8].Text;
                String myStage = griddataxschools.Rows[j].Cells[9].Text;
                String myFirstLanguage = griddataxschools.Rows[j].Cells[10].Text;
                String mySecondLanguage = griddataxschools.Rows[j].Cells[11].Text;

                TransferRequestXSchools.Add(new SD_Transfer_Request_Schools()

                {

                    TransferRequestId = mytrid,
                    SchoolName = mySchoolName,
                    SatrtDate = mySatrtDate,
                    EndDate = myEndDate,
                    Address = myAddress,
                    Certification = myCertification,
                    Grade = myGrade,
                    Stage = myStage,

                    FirstLanguage = myFirstLanguage,
                    SecondLanguage = mySecondLanguage,

                }
                    );

            }
        }


        # endregion



        # region " Search *********************"

        //***************** This is function to search by  Get All btn ***************************
        //-**************************************************************************
        protected void btnGetAll_Click(object sender, EventArgs e)
        {
            InitializeControls();

            GridDataBind();
        }



        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GridDataBind();
        }

        # endregion

        protected void ddlCertificate_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertificate_Id.SelectedIndex > 0)
            {
                ddlStage_Id.Items.Clear();
                ddlGrade_Id.Items.Clear();

                ddlStage_Id.DataSource = Structure.GetStagesByCertificate(Convert.ToInt64(ddlCertificate_Id.SelectedValue));
                ddlStage_Id.DataTextField = "Name";
                ddlStage_Id.DataValueField = "id";
                ddlStage_Id.DataBind();
                ddlStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlStage_Id.Enabled = true;
                ddlGrade_Id.Enabled = false;


            }

        }

        protected void ddlStage_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlStage_Id.SelectedIndex > 0)
            {
                ddlGrade_Id.Items.Clear();
                ddlGrade_Id.DataSource = Structure.GetGradesByStage(Convert.ToInt64(ddlStage_Id.SelectedValue));
                ddlGrade_Id.DataTextField = "Name";
                ddlGrade_Id.DataValueField = "id";
                ddlGrade_Id.DataBind();
                ddlGrade_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlGrade_Id.Enabled = true;
            }
        }


        protected void ddlcampus_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlcampus_Id.SelectedIndex > 0)
            {

                ddlCertificate_Id.Items.Clear();
                ddlStage_Id.Items.Clear();
                ddlGrade_Id.Items.Clear();

                ddlCertificate_Id.DataSource = Structure.GetCertificatesByCampus(Convert.ToInt64(ddlcampus_Id.SelectedIndex));
                ddlCertificate_Id.DataTextField = "Name";
                ddlCertificate_Id.DataValueField = "Id";
                ddlCertificate_Id.DataBind();
                ddlCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

                ddlCertificate_Id.Enabled = true;
                ddlStage_Id.Enabled = false;
                ddlGrade_Id.Enabled = false;

            }
        }

        protected void btnXSchooltransferrequest_Click(object sender, EventArgs e)
        {
            pnlbasicadd.Visible = false;
            pnlAddNewXSchools.Visible = true;
        }

        protected void btncancelxschool_Click(object sender, EventArgs e)
        {
            pnlAddNewXSchools.Visible = false;
            pnlbasicadd.Visible = true;

        }

        protected void ddlCertification_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (ddlCertification.SelectedValue != "0")
            {
                ddlGrade.Items.Clear();
                ddlsatge.Items.Clear();
                ddlGrade.DataSource = Structure.GetStagesByCertificate(long.Parse(ddlCertification.SelectedValue));
                ddlGrade.DataTextField = "Name";
                ddlGrade.DataValueField = "Id";
                ddlGrade.DataBind();
                ddlGrade.Enabled = true;
                ddlsatge.Enabled = false;

            }

        }

        protected void ddlGrade_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (ddlGrade.SelectedValue != "0")
            {
                ddlsatge.Items.Clear();

                ddlsatge.DataSource = Structure.GetGradesByStage(long.Parse(ddlGrade.SelectedValue));
                ddlsatge.DataTextField = "Name";
                ddlsatge.DataValueField = "Id";
                ddlsatge.DataBind();
                ddlsatge.Enabled = true;
            }
        }

        protected void btnaddtogrid_Click(object sender, EventArgs e)
        {

            dt = new DataTable("Xschools");

            DataColumn col = dt.Columns.Add("id", typeof(Int32));
            col.AllowDBNull = false;
            col.Unique = true;


            dt.Columns.Add("Student_Id", typeof(long));
            col.AllowDBNull = false;

            dt.Columns.Add("اسم المدرسة", typeof(String));
            dt.Columns.Add("الفترة من", typeof(DateTime));
            dt.Columns.Add("الفترة الى", typeof(DateTime));
            dt.Columns.Add("العنوان", typeof(String));
            dt.Columns.Add("الشهادة", typeof(String));
            dt.Columns.Add("المرحلة", typeof(String));
            dt.Columns.Add("الصف", typeof(String));
            dt.Columns.Add("اللغة الاولى", typeof(String));
            dt.Columns.Add("اللغة الثانية", typeof(String));

            DataRow row;

            row = dt.NewRow();

            if (Session["dt"] != null)
            {
                int x = 0;
                for (i = 0; i <= ((DataTable)Session["dt"]).Rows.Count - 1; i++)
                {

                    x = x + 1;

                }

                row["id"] = x + 1;
            }

            else
            {
                row["id"] = 1;


            }

            row["اسم المدرسة"] = txtSchoolName.Text;
            DateTime dfrom = Convert.ToDateTime(txtSatrtDate.Text);
            row["الفترة من"] = txtSatrtDate.Text != "" ? dfrom.ToShortDateString() : DateTime.Now.ToShortDateString();

            DateTime dto = Convert.ToDateTime(txtEndDate.Text);
            row["الفترة الى"] = txtEndDate.Text != "" ? dto.ToShortDateString() : DateTime.Now.ToShortDateString();


            row["العنوان"] = txtAddress.Text;
            row["الشهادة"] = ddlCertification.SelectedItem.Text;
            row["المرحلة"] = ddlGrade.SelectedItem.Text;
            row["الصف"] = ddlStage_Id.SelectedItem.Text;
            row["اللغة الاولى"] = ddlFirstLanguage.SelectedItem.Text;
            row["اللغة الثانية"] = ddlSecondLanguage.SelectedItem.Text;
            dt.Rows.Add(row);


            if (Session["dt"] != null)
            {
                dt.Merge((DataTable)Session["dt"]);
            }
            Session["dt"] = dt;



            griddataxschools.DataSource = dt;
            griddataxschools.DataBind();

            clearmyschool();
        }

        protected void clearmyschool()
        {

            ////////************************** sxchools**********************
            txtSatrtDate.Text = "";
            txtEndDate.Text = "";
            txtSchoolName.Text = "";
            txtAddress.Text = "";
            ddlCertification.SelectedIndex = 0;
            ddlGrade.SelectedIndex = 0;
            ddlStage_Id.SelectedIndex = 0;
            ddlFirstLanguage.SelectedIndex = 0;
            ddlSecondLanguage.SelectedIndex = 0;

        }

        protected void griddataxschools_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "OnDelete")
            {
                GridViewRow selectedRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                DataKey shoolid = griddataxschools.DataKeys[selectedRow.RowIndex];

                if (Session["dt"] != null)
                {
                    ((DataTable)Session["dt"]).Rows[selectedRow.RowIndex].Delete();

                }
            }
            griddataxschools.DataSource = Session["dt"];
            griddataxschools.DataBind();
        }

        protected void griddataxschools_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            e.Row.Cells[1].Visible = false;
            e.Row.Cells[2].Visible = false;


        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            //update status in t r table from 1 to 2 
            AcceptOfStudents();
        }

        protected void AcceptOfStudents()
        {

            int i;
            for (i = 0; (i <= (griddata.Rows.Count - 1)); i++)
            {
                if (griddata.Rows[i].Visible == true)
                {
                    CheckBox AcceptStudent;
                    AcceptStudent = ((CheckBox)(griddata.Rows[i].FindControl("CheckBox1")));

                    if ((AcceptStudent.Checked == true))
                    {

                        Int64 trid;
                        Label lbltrid;
                        lbltrid = ((Label)(griddata.Rows[i].FindControl("lbltrid")));
                        trid = Convert.ToInt64(lbltrid.Text);

                        ADM_Transfer_Request mytrrecord = TransferRequest.Single(trid);


                        /////////////////Update in transfer request  and add paymant in transefer request table

                        ADM_Transfer_Request mytarnsferrequest = new ADM_Transfer_Request();
                        mytarnsferrequest.Id = trid;
                        mytarnsferrequest.Payment_Order_Number = null;
                        mytarnsferrequest.Price = null;
                        mytarnsferrequest.Status = 2;

                        updatestatuspaymant (i);

                        griddata.Rows[i].Visible = false;

                    }
                }

                lblRecordNo.Text = "";
                lblMessage.Text = "لقد تمت عملية القبول بنجاح";


            }


        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            //update status in t r table from 1 to 3 

            RejectStudent();
        }


        protected void RejectStudent()
        {

            int i;
            for (i = 0; (i <= (griddata.Rows.Count - 1)); i++)
            {
                if (griddata.Rows[i].Visible == true)
                {

                    CheckBox AcceptStudent;
                    AcceptStudent = ((CheckBox)(griddata.Rows[i].FindControl("CheckBox1")));

                    if ((AcceptStudent.Checked == true))
                    {

                        Int64 trid;
                        Label lbltrid;
                        lbltrid = ((Label)(griddata.Rows[i].FindControl("lbltrid")));
                        trid = Convert.ToInt64(lbltrid.Text);

                        ADM_Transfer_Request mytrrecord = TransferRequest.Single(trid);


                        /////////////////Update in transfer request  and add paymant in transefer request table

                        ADM_Transfer_Request mytarnsferrequest = new ADM_Transfer_Request();
                        mytarnsferrequest.Id = trid;
                        mytarnsferrequest.Payment_Order_Number = null;
                        mytarnsferrequest.Price = null;
                        mytarnsferrequest.Status = 3;

                        TransferRequest.UpdateStatus(mytarnsferrequest);

                        griddata.Rows[i].Visible = false;

                    }
                }
                lblRecordNo.Text = "";
                lblMessage.Text = "لقد تمت عملية الرفض بنجاح";


            }


        }

        protected void ddlSearchcampus_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSearchcampus_Id.SelectedIndex > 0)
            {
                ddlSearchCertificate_Id.Items.Clear();
                ddlSearchStage_Id.Items.Clear();
                ddlSearchGrade_ID.Items.Clear();

                ddlSearchCertificate_Id.DataSource = Structure.GetCertificatesByCampus(Convert.ToInt64(ddlSearchcampus_Id.SelectedIndex));
                ddlSearchCertificate_Id.DataTextField = "Name";
                ddlSearchCertificate_Id.DataValueField = "Id";
                ddlSearchCertificate_Id.DataBind();
                ddlSearchCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

                ddlSearchCertificate_Id.Enabled = true;
                ddlSearchGrade_ID.Enabled = false;
                ddlSearchStage_Id.Enabled = false;
            }
        }

        protected void ddlSearchCertificate_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSearchCertificate_Id.SelectedIndex > 0)
            {
                ddlSearchStage_Id.Items.Clear();
                ddlSearchGrade_ID.Items.Clear();

                ddlSearchStage_Id.DataSource = Structure.GetStagesByCertificate(Convert.ToInt64(ddlSearchCertificate_Id.SelectedValue));
                ddlSearchStage_Id.DataTextField = "Name";
                ddlSearchStage_Id.DataValueField = "id";
                ddlSearchStage_Id.DataBind();
                ddlSearchStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

                ddlSearchStage_Id.Enabled = true;
                ddlSearchGrade_ID.Enabled = false;


            }
        }

        protected void ddlSearchStage_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSearchStage_Id.SelectedIndex > 0)
            {

                ddlSearchGrade_ID.Items.Clear();
                ddlSearchGrade_ID.DataSource = Structure.GetGradesByStage(Convert.ToInt64(ddlSearchStage_Id.SelectedValue));
                ddlSearchGrade_ID.DataTextField = "Name";
                ddlSearchGrade_ID.DataValueField = "id";
                ddlSearchGrade_ID.DataBind();
                ddlSearchGrade_ID.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlSearchGrade_ID.Enabled = true;


            }
        }

        protected void cbStatus_CheckedChanged(object sender, EventArgs e)
        {
            if (cbStatus.Checked == true)
            {

                GridDataBind();
                btnAccept.Visible = false;
                btnReject.Visible = false;

            }

            else
            {
                GridDataBind();

            }

        }

        protected void updatestatuspaymant(int rowindex)
        {
            //change satus from 2 to 4 and creat paymant no.

            //int i;
            //for (i = 0; (i <= (griddata.Rows.Count - 1)); i++)
            //{
            if (griddata.Rows[rowindex].Visible == true)
            {


                Int64 trid;
                Label lbltrid;
                lbltrid = ((Label)(griddata.Rows[rowindex].FindControl("lbltrid")));
                trid = Convert.ToInt64(lbltrid.Text);

                ADM_Transfer_Request mytrrecord = TransferRequest.Single(trid);

                long poid = Structure.GetPayment_Order_Number();


                long ExpenseId = TransferRequest.Expen_ID("100");

                Decimal myprice = FileOrder.Expense_Value(ExpenseId);
                /////////////////Add to Fileorder and creat file order 

                Int64 myfileorderid = FileOrder.Add(new ADM_File_Order()

                {
                    Payment_Order_Number = poid,
                    FirstName = mytrrecord.FirstName,
                    SecondName = mytrrecord.SecondName,
                    ThirdName = mytrrecord.ThirdName,
                    FourthName = mytrrecord.FourthName,
                    Date = mytrrecord.Date,

                    Campus_Id = mytrrecord.Campus_Id,
                    Certificate_Id = mytrrecord.Certificate_Id,
                    Grade_Id = mytrrecord.Grade_Id,
                    Stage_Id = mytrrecord.Stage_Id,
                    Price = myprice

                }
                );

                mytrrecord.Id = trid;
                mytrrecord.Payment_Order_Number = poid;
                mytrrecord.Price = myprice;
                mytrrecord.Status = 4;

                TransferRequest.UpdateStatus(mytrrecord);

                griddata.Rows[rowindex].Visible = false;


                //************************** fill order panel 

                Session["FileOrderId"] = poid;

                string strbScript = "";
                strbScript = "<script language='javascript'>";
                strbScript = strbScript + "window.open('Reports/Showreport.aspx', true, 'height=600,width=800,status=no,toolbar=no,menubar=no,location=no')";
                strbScript = strbScript + "</script>";

                Response.Write(strbScript);
                //ADM_File_Order myorderid = FileOrder.Single(poid);

                //txtShowPaymentNo.Text = myorderid.Payment_Order_Number.ToString();
                //txtShowPrice.Text = myorderid.Price.ToString();
                //txtShowName.Text = myorderid.FirstName + " " + myorderid.SecondName + " " + myorderid.ThirdName + " " + myorderid.FourthName;
                //txtShowDate.Text = myorderid.Date.ToShortDateString().ToString();

                //txtShowCampus.Text = myorderid.ORG_Campus.Name;
                //txtShowCertificate.Text = myorderid.ORG_Certificate.Name;
                //txtShowStage.Text = myorderid.ORG_Stage.Name;
                //txtShowGrade.Text = myorderid.ORG_Grade.Name;



                //pnlAdd.Visible = false;

                //PrntLabel.Visible = true;
                //pnlorder.Visible = true;
                //PrntLabel.Visible = true;
                //lblRecordNo.Text = "";
                //lblMessage.Text = "لقد تمت امر الدفع بنجاح";
            }
        }

        protected void griddata_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;
            if (e.CommandName == "paymant")
            {

                if (griddata.Rows[row.RowIndex].Visible == true)
                {

                    //Button btnpaymant = (Button)griddata.Rows[i].Cells[5].FindControl("btncreatpaymantandprint");
                    //btnpaymant.Click += new EventHandler(btnpaymant_Click);

                    updatestatuspaymant(row.RowIndex);
                }


            }
        }



    }

}

